package com.hsq.androidproject.ui.recommend_fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.adapter.MyGridViewStartAdapter;
import com.hsq.androidproject.entity.rtv;
import com.hsq.androidproject.utils.OkHttpClientManager;
import com.squareup.okhttp.Request;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class RTvFragment extends Fragment {
    public List<rtv> list = new ArrayList<>();
    MyGridViewStartAdapter myGridViewStartAdapter;
    GridView g;

    public RTvFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_rtv, container, false);
        g = (GridView) view.findViewById(R.id.rtv);
        OkHttpClientManager.getAsyn("http://api.hanju.koudaibaobao.com/api/series/indexV2?offset=0&count=48", new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
            }

            @Override
            public void onResponse(String response) {
                //rtvbean
                try {
                    JSONObject js = new JSONObject(response);
                    rtv rs;
                    JSONArray array = js.getJSONArray("seriesList");
                    for (int i = 0; i < array.length(); i++) {
                        js = array.getJSONObject(i);
                        rs = new rtv();
                        rs.setIsFinished(js.getString("isFinished"));
                        rs.setId(js.getString("sid"));
                        rs.setBitmap(js.getString("thumb"));
                        rs.setSource(js.getString("source"));
                        rs.setName(js.getString("name"));
                        rs.setCount(js.getInt("count"));
                        rs.setRank(js.getInt("rank"));
                        list.add(rs);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                myGridViewStartAdapter = new MyGridViewStartAdapter(getContext(), list);
                g.setAdapter(myGridViewStartAdapter);
            }
        });
        return view;
    }
}
